package cn.acewill.yxd.yunxundian.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.model.CollegueSchedule;
import cn.acewill.yxd.yunxundian.model.Schedule;

/**
 * Created by xxx on 2018/2/9.
 */

public class CollegueAdapter extends RecyclerView.Adapter<CollegueAdapter.ViewHolder>{

    private Context mContext;
    private List<CollegueSchedule> csList;
    private OnBtnClickListener btnClickListener;
    private ColFilter filter;

    public CollegueAdapter(Context mContext, List<CollegueSchedule> csList,OnBtnClickListener btnClickListener){
        this.mContext = mContext;
        this.csList = csList;
        this.btnClickListener = btnClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.new_evlation_item_view,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        final CollegueSchedule cs = csList.get(position);
        holder.bigTag(cs);
        holder.ingArea.setVisibility(View.GONE);
        holder.overArea.setVisibility(View.VISIBLE);
        holder.taskOA.setText("OA");
        holder.taskType.setText(cs.getReportName());
        holder.shopName.setText(cs.getShopname());
        holder.taskScore.setText(cs.getScore()+"");
        holder.taskTime.setText("");
        holder.shareTask.setVisibility(View.GONE);
        holder.sendMail.setVisibility(View.GONE);
        holder.shareTask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnClickListener.shareListener(cs);
            }
        });
        holder.lookTask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnClickListener.lookListener(cs);
            }
        });

        //发送与提交同一个activity
        holder.sendMail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnClickListener.sendMailListener(cs);
            }
        });


    }

    @Override
    public int getItemCount() {
        return csList!=null?csList.size():0;
    }

    public Filter setFilter(){
        if (filter==null){
            filter = new ColFilter(this,csList);
        }
        return filter;
    }



    class ViewHolder extends RecyclerView.ViewHolder {
        TextView shopName;
        TextView taskStatus;
        TextView taskOA;
        TextView taskScore;
        TextView taskType;
        TextView taskTime;
        TextView doTask;
        TextView shopSign;
        TextView submitData;
        TextView shareTask;
        TextView lookTask;
        TextView taskTimeType;
        TextView sendMail;
        LinearLayout overArea;
        LinearLayout ingArea;

        public ViewHolder(View itemView) {
            super(itemView);
            shopName = (TextView) itemView.findViewById(R.id.shopName);
            taskStatus = (TextView) itemView.findViewById(R.id.taskStatus);
            taskScore = (TextView) itemView.findViewById(R.id.taskScore);
            taskType = (TextView) itemView.findViewById(R.id.taskType);
            taskOA = itemView.findViewById(R.id.taskOA);
            taskTime = (TextView) itemView.findViewById(R.id.taskTime);
            doTask = (TextView) itemView.findViewById(R.id.doTask);
            shopSign = (TextView) itemView.findViewById(R.id.shopSign);
            submitData = (TextView) itemView.findViewById(R.id.submitData);
            shareTask = (TextView) itemView.findViewById(R.id.shareTask);
            lookTask = (TextView) itemView.findViewById(R.id.lookTask);
            sendMail = itemView.findViewById(R.id.sendMail);
            taskTimeType = (TextView) itemView.findViewById(R.id.taskTimeType);
            overArea = (LinearLayout) itemView.findViewById(R.id.overArea);
            ingArea = (LinearLayout) itemView.findViewById(R.id.ingArea);

            submitData.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    btnClickListener.submitListener((CollegueSchedule) submitData.getTag());
                }
            });
            shareTask.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    btnClickListener.shareListener((CollegueSchedule) shareTask.getTag());
                }
            });
            lookTask.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    btnClickListener.lookListener((CollegueSchedule) lookTask.getTag());
                }
            });
            sendMail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    btnClickListener.sendMailListener((CollegueSchedule)sendMail.getTag());
                }
            });
        }
        public void bigTag(CollegueSchedule cs){
            submitData.setTag(cs);
            shareTask.setTag(cs);
            lookTask.setTag(cs);
            sendMail.setTag(cs);
        }
    }
    public interface OnBtnClickListener{
        void submitListener(CollegueSchedule cs);
        void shareListener(CollegueSchedule cs);
        void lookListener(CollegueSchedule cs);
        void sendMailListener(CollegueSchedule cs);
    }

    public class ColFilter extends Filter{

        private CollegueAdapter adapter;
        private List<CollegueSchedule> dataList;
        private List<CollegueSchedule> filterList;

        private ColFilter(CollegueAdapter adapter,List<CollegueSchedule> clList){
            this.adapter = adapter;
            this.dataList = clList;
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            filterList = new ArrayList<>();
            FilterResults results = new FilterResults();
            if (constraint != null && constraint.length() > 0) {
                for (CollegueSchedule cs : dataList) {
                    String time = cs.getRealendtime().replaceAll("-", "").replace("T", " ");
                    if (time.contains(constraint) || cs.getShopname().contains(constraint)) {
                        filterList.add(cs);
                    }
                }

            }else{
                filterList.addAll(dataList);
            }
            results.values = filterList;
            results.count = filterList.size();
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            List<CollegueSchedule> valueList = (List<CollegueSchedule>) results.values;
            adapter.csList = valueList;
            adapter.notifyDataSetChanged();

        }
    }
}
